import Foundation

struct Info: Codable {
    struct Jokes: Codable {
        let totalCount: Int
        let idRange: [String: [Int]]
        let safeJokes: [SafeJoke]
        var langCount: [String : Int] {
            idRange.mapValues { range in
                guard !range.isEmpty else { return 0 }
                return range.last! + 1
            }
        }
    }
    let jokes: Jokes
    struct SafeJoke: Codable {
        let lang: String
        let count: Int
    }
}

func getInfo() async throws -> Info {
    let url = "https://v2.jokeapi.dev/info"
    let apiService = APIService(urlString: url)
    do {
        return try await apiService.getJSON()
    } catch {
        throw error
    }
}

Task {
    do {
        let info = try await getInfo()
        print(info.jokes.totalCount)
        for safeItem in info.jokes.safeJokes {
            let langTotal = info.jokes.langCount[safeItem.lang] ?? 0
            print(safeItem.lang, safeItem.count, langTotal)
        }
    } catch {
        print(error.localizedDescription)
    }
}
